<?php 

namespace Home\Controller;
use Think\Controller;

class RegisterController extends Controller {

    public function index(){
    	
    	//注册判断，前端验证数据长短，后端验证用户名是否存在以及验证码是否正确
    	if(IS_POST){

    		if(I('post.agreement_chk')!=null&&I('post.agreement_chk')=='agree'&&I('post.username')!=null&&I('post.email')!=null&&I('post.password')!=null){
    			//验证码验证
    			$seccode_verify = I('post.seccode_verify');
    			$this->check_verify($seccode_verify);

				$username = I('post.username');
	   			$email = I('post.email');
	   			//想将密码设置的更保险，可以接一个常量，一同加密到数据库
	   			$password = md5(I('post.password'));
	   			$sex = I('post.sex');
	   			$jobs = I('post.jobs');
	   			$province = I('post.selectp');
	   			$city = I('post.selectc');
	   			$introduce = I('post.signature');
	   			

	   			$user = M('user');

	   			$sql = "SELECT * FROM `user` WHERE `username` = '$username' OR `email` = '$email'";

	   			$query = $user->query($sql);

	   			if ($query) {
	   				$this->error('用户名或者邮箱已经存在',U('Home/Register/index'));
	   			}else{

	   				$regist_time = time();	  
	   				$mysql = "INSERT INTO `user`(`username`,`password`,`email`,`sex`,`jobs`,`province`,`city`,`introduce`,`regist_time`)VALUES('$username','$password','$email','$sex','$jobs','$province','$city','$introduce','$regist_time')";

		   			$a = $user->execute($mysql);
		   			if($a){
		   				$sql = "SELECT `id`,`score` from `user` WHERE `username` = '$username'";

						$user = M('user');

						$query = $user -> query($sql);

		   				$_SESSION['username']=$username;
						//用户id
						$_SESSION['id']=$query[0]['id'];
						//用户真实姓名
						$_SESSION['truename']=$truename;
						//积分
						$_SESSION['score']=$query[0]['score'];	

		   				$this->success('注册成功,即将为您跳转至首页！',U('Home/Index/index'));
		   				return;
		   			}

	   			}

    		}else{
    			$this->error();

    		}

    	}
    	$this->display();
    }

    private function check_verify($code, $id = ''){
    	//验证验证码
	    $verify = new \Think\Verify();
	    $check = $verify->check($code, $id);

	    if (!$check) {
	    	$this->error('注册信息或者验证码错误');
	    }
	}

    public function code(){

    	$config =    array(
    		'imageW' => 100,
    		'imageH' => 40,
	        'fontSize'    =>    20,    // 验证码字体大小
	        'length'      =>    3,     // 验证码位数
	        'useNoise'    =>    false, // 关闭验证码杂点
			);
	    $Verify =  new \Think\Verify($config);
	    $Verify->entry();

    }

}













 ?>